<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/7/17 0017
 * Time: 15:14
 */

namespace Home\Model;


use Think\Model;

class PageModel extends Model
{
    protected $autoCheckFields =false;//设置为虚拟模型

    public $lang = 9;//显示页码个数
    /*
     * $url,        //挑战链接
     * $count,      //总记录数
     * $num,        //每页几条
     * $in_page     //当前页码
    */
    //生成分页
    public function createpage($url,$count,$num,$in_page){

        $page = ceil($count/$num);//页数
        $up = $in_page-1 > 0 ? $in_page-1:$in_page;//上一页
        $down = $in_page+1 <= $page ? $in_page+1:$in_page;//下一页

        if($up != $in_page) {
            $str = '<a href="' . $url . '/p/1">首页</a><a href="' . $url . '/p/' . $up . '">上一页</a>';
        }else{
            $str = '<a href="' . $url . '/p/1">首页</a><a>上一页</a>';
        }//end if

        $page_arr = array();
        for($i = 1; $i <= $page;$i++){
            $page_arr[]=$i;
        }//end for

        if($page<=$this->lang){
            $lang = $page;
        }else{
            $lang = $this->lang;
        }//end if

        $s_lang = floor($lang/2);//显示页码个数

        $start = $in_page-$s_lang > 0 ? $in_page-$s_lang-1 : 0;

        $start = $in_page+$s_lang >= $page ? $page-$lang :$start;


        $page_arr = array_slice($page_arr,$start,$lang);
        $str_url = '';
        foreach($page_arr as $k => $v){
            if($v == $in_page){
                $str_url .= '<a style="background-color:#008ee1;color:#FFFFFF;">'.$v.'</a>';
            }else{
                $str_url .= '<a href="'.$url.'/p/'.$v.'">'.$v.'</a>';
            }//end if
        }//end if

        //默认存在页码
        if($str_url == ''){
            $str_url = '<a style="background-color:#008ee1;color:#FFFFFF;">1</a>';
        }

        $str .= $str_url;
        if($down != $in_page) {
            $str .= '<a href="' . $url . '/p/' . $down . '">下一页</a><a href="' . $url . '/p/' . $page . '">末页</a>';
        }else{
            $str .= '<a>下一页</a><a href="' . $url . '/p/' . $page . '">末页</a>';
        }
        return $str;
    }//end fun
}//end class